* Dickstein, Ho, and Mark (2023)
* This file estimates the premium setting equation for each bootstrap draw

* PART ONE: Defining a function that does the work

cap program drop paper_present_bootstrap
program define paper_present_bootstrap
        args data_path data_path_bootstrap tab_out cost_var k1
        load_pred_diag_data_bootstrap $data_path_bootstrap "_indiv"

        collapse (mean) weighted_cbar grossp *mean* indiv_sub* all_sub* (sum) sum_s_ij payeryear_sum_s_ij s_a_jt s_grossp `cost_var' admin_sum sec3_sum sec4_sum sec5_sum s_sec3_jt  s_sec4_jt  s_sec5_jt (sum) num_subsidized subscriberid, by(best_guess_ra payer_code nominal_av year)

        bysort year best_guess_ra: egen sum_subsidized = sum(num_subsidized)
        bysort year best_guess_ra: egen sum_subscribers = sum(subscriberid)
        bysort year best_guess_ra: gen year_ra_subsidized = sum_subsidized / sum_subscribers

        sort best_guess_ra payer_code nominal_av
        bysort best_guess_ra year nominal_av: gen num_plans_in_market = _N
        egen plan = group(best_guess_ra payer_code nominal_av)
        xtset plan year
        by plan (year): gen lag_admin_sum = admin_sum[_n-1]
        by plan (year): gen lag_sec3_sum = sec3_sum[_n-1]
        by plan (year): gen lag_sec4_sum = sec4_sum[_n-1]
        by plan (year): gen lag_sec5_sum = sec5_sum[_n-1]
        by plan (year): gen s_lag_a_jt = (lag_admin_sum/12) * (sum_s_ij / payeryear_sum_s_ij) / 100
        by plan (year): gen sec3_lag_a_jt = (lag_sec3_sum/12) * (sum_s_ij / payeryear_sum_s_ij) / 100
        by plan (year): gen sec4_lag_a_jt = (lag_sec4_sum/12) * (sum_s_ij / payeryear_sum_s_ij) / 100
        by plan (year): gen sec5_lag_a_jt = (lag_sec5_sum/12) * (sum_s_ij / payeryear_sum_s_ij) / 100

        by plan (year): gen lag_year_ra_subsidized = year_ra_subsidized[_n-1]
        by plan (year): gen lag_num_plans_in_market = num_plans_in_market[_n-1]
        *by plan (year): gen lag_all_subs0 =  all_subs0[_n-1] // out of state subscriber
        *by plan (year): gen lag_indiv_subs0 =  indiv_subs0[_n-1] // out of state subscriber

        gen kaiser = (payer_code == 7)
        local market = "best_guess_ra"
        bysort best_guess_ra year nominal_av: egen kaiser_in_mkt = max(kaiser)

        label var s_grossp "Premiums"
        label var `cost_var' "Medical costs"
        label var s_lag_a_jt "Administrative costs (t-1)"
        eststo clear
        eststo: reg s_grossp `cost_var' if year == 2015 | year == 2016, r
        eststo: reg s_grossp `cost_var' i.payer_code#i.year  if year == 2015 | year == 2016, r
        eststo: ivreg2 s_grossp (`cost_var'=num_plans_in_market year_ra_subsidized) i.payer_code##i.year  if year == 2015 | year == 2016, r ffirst saverf
        local st3 = `e(widstat)'
        eststo: ivreg2 s_grossp (`cost_var' s_lag_a_jt = num_plans_in_market year_ra_subsidized lag_num_plans_in_market lag_year_ra_subsidized)  i.year, r ffirst saverf 
        local st4 = `e(widstat)'
        estadd local mwidstat `st3': est3
        estadd local mwidstat `st4': est4
        esttab using "`tab_out'/pr_paper_best_guess_ra_`cost_var'_`k1'.csv", ///
                stats(N mwidstat r2, label("N" "1st-stage F-stat" "\$R^2\$" )) nostar ///
                label drop(*year* *payer* _cons)  se r2 nomtitles       ///
                replace  plain

        esttab using "`tab_out'/pr_paperfull_best_guess_ra_`cost_var'_`k1'.csv", ///
                 nostar ///
                label  se r2 nomtitles  ///
                replace  plain

        estadd local payeryear "": est1 est4
        estadd local payeryear "\checkmark": est2 est3
        estadd local year "": est1 est2 est3
        estadd local year "\checkmark": est4

        esttab using "`tab_out'/pr_paper_best_guess_ra_`cost_var'_`k1'.tex", ///
                stats(year payeryear N r2, label("Payer FEs" "Payer-Year FEs" "N" "\$R^2\$")) nostar ///
                label drop(*year* *payer* _cons)  se r2 nomtitles       ///
                title("Premium setting equation") replace

end

* PART TWO: Run the loop that calls this function and estimates the premium setting equation for each bootstrap call:

** Prelims
global data_path = "../output"
global tab_out = "release"
mkdir release
qui do ../helpers.do

** Running the loop
forval k=1/20 { 
  global main_seed = 4197169
  global data_path_bootstrap = "./output/predicted_prem_4197169_boot_`k'.dta"
  paper_present_bootstrap $data_path $data_path_bootstrap $tab_out s_wsimcost `k'
}

